<?php
	include 'header.php';
?>
	<center><h3>"Create a new account"</h3>
	<form method="POST">
		<div>Surname:<input type="text" name="surname"/></div><br />
		<div>Name:<input type="text" name="name"/></div><br />
		<div>Father name:<input type="text" name="father_name"/></div><br />
		<div>Username<input type="text" name="user_name"/></div><br />
		<div>Password<input type="password" name="password"/></div><br />
		<div>e-mail<input type="text" name="e-mail"/></div><br />
		<div><input type="submit" value="Create"/></div>
	</form>
	<?php
		$connect=mysqli_connect('localhost','hristo9153','1111111','users',3306);
		mb_internal_encoding('utf-8');
		mysqli_set_charset($connect,'utf8');
		if($_POST){
			$ts=time();
			$ts+=3600;
			$date_format='Y-m-d h-i-s';
			$date_registered=date($date_format,$ts);
			$surname=trim($_POST['surname']);
			$name=trim($_POST['name']);
			$father_name=trim($_POST['father_name']);
			$user_name=trim($_POST['user_name']);
			$password=trim($_POST['password']);
			$email=trim($_POST['e-mail']);
			$error=0;
			if(mb_strlen($surname)<3){
				echo '<p>Surname is too short</p>';
				$error=1;
			}
			if(mb_strlen($name)<3){
				echo '<p>The name is too short</p>';
				$error=1;
			}
			if(mb_strlen($father_name)<3){
				echo '<p>The father name is too short</p>';
				$error=1;
			}
			if(mb_strlen($user_name)<3){
				echo '<p>The user name is too short</p>';
				$error=1;
			}
			if(mb_strlen($password)<3){
				echo '<p>The password is too short</p>';
				$error=1;
			}
			
			if(!$error){
				$q_select='SELECT * FROM users where user_name="'.$user_name.'" AND password="'.$password.'"';
				$query=mysqli_query($connect,$q_select);
				if(!$query){
					echo mysql_error($connect);
				}
				if($query->num_rows == 0){
					$q_save='INSERT INTO users(user_name,password,surname,name,father_name,email,date_registered)
					VALUES("'.$user_name.'","'.$password.'","'.$surname.'","'.$name.'","'.$father_name.'","'.$email.'","'.$date_registered.'")';
					$query=mysqli_query($connect,$q_save);
					echo mysqli_error($connect);
					echo 'insert into database';
				}
			}
		}
	?>
	<a href="insert.php">"Insert orders"</a><br />
	<a href="list_of_orders.php">"List of orders"</a><br />
	<a href="session_destroy.php">"EXIT"</a>
<?php
	include 'footer.php';
?>